function IntegralActivity() {
    $.ajax({

        url:root.integralManagement.searchIntegralExchangeList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
              "skuName":$("#skuName").val(),
              "skuCode":$("#skuCode").val(),
              "status":$("#status").val(),
              "startTime":$("#timemin").val(),
              "endTime":$("#timemax").val(),
              "createPerson":$("#createPerson").val(),
              "pageSize":"30",
              "pageNum":"1"
        }),
        success: function (data) {
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        /* root.member.adminMember2CList*/
                        url: root.integralManagement.searchIntegralExchangeList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "skuName":$("#skuName").val(),
                            "skuCode":$("#skuCode").val(),
                            "status":$("#status").val(),
                            "startTime":$("#timemin").val(),
                            "endTime":$("#timemax").val(),
                            "createPerson":$("#createPerson").val(),
                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){

    // var j = 0;
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


    if(data !=null && data != ''){
        var datashu =data.startRow+1;


        $.each(data.data, function (i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.skuIntegral);
            rowData.push(field.skuCode);
            rowData.push(field.skuName);
            rowData.push(field.skuUnit);
            rowData.push(field.startTime);
            rowData.push(field.endTime);
            rowData.push(field.skuStatus);
            rowData.push(field.productCount);
            rowData.push(field.productTotal);
            rowData.push(field.createPerson);
            rowData.push(field.activityId);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val=""
                        if(row[6]=="1001-01-01 00:00:00"){
                            val = '<span>'+row[5]+'</span>~<span>不限</span>'
                        }else {
                            val = '<span>'+row[5]+'</span>~<span>'+row[6]+'</span>'
                        }


                        return val;
                    },
                    "targets": 5
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if(row[7]==10){
                            val='进行中'
                        }
                        else if(row[7]==20){
                            val='未生效'
                        }else if(row[7]==30){
                            val='已结束'
                        }else if(row[7]==40){
                            val='已禁用'
                        }
                        return val;
                    },
                    "targets":6
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":7
                },
                {
                    /*虚拟库存上限*/
                    "render": function(data, type, row, meta) {
                        var isSpecialSku = row[9]==null?true:false ;
                        var classStyle = isSpecialSku?"Hui-iconfont f-ricon-edit  hide":"Hui-iconfont f-ricon-edit" ;

                        var val = '<span id="' + row[0] + '">' + row[9] + '</span>&nbsp;<input value="' + row[9] + '" class="hide"  id="changeNum' + row[0] + '" oninput=\'this.value=this.value.replace(/^[0]+[0-9]*$/gi,"")\' onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')">' +
                            '<span id="editorIcon'+row[0]+'" class="'+classStyle+'" onclick="edit_stock(\''+row[0]+'\')">&#xe6df; ' +
                            '</span>  <button id="change'+row[0]+'" type="button" onclick="isChange(\''+row[0]+'\',\''+row[8]+'\',\''+row[11]+'\')" class="hide" >确定</button> ' +
                            ' <button id="cancel'+row[0]+'"  type="button"  class="hide" onclick="isCancel(\''+row[0]+'\')">取消</button>';
                        return val;

                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[10]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function (data, type, row, meta) {
                        var val =""
                        if(row[7]==30){
                            val ='<a style="margin-right: 14px" onclick="deletes(\'' + row[2] + '\',\'' +row[7] + '\',\'' +row[11] + '\')">删除</a><a style="margin-right: 14px;color: slategrey;">禁用</a><a onclick="editPoint(\'' + row[11] + '\')" style="margin-right: 14px">编辑</a>'

                        }else if (row[7]==40){
                            val ='<a style="margin-right: 14px" onclick="deletes(\'' + row[2] + '\',\'' +row[7] + '\',\'' +row[11] + '\')">删除</a> <a style="margin-right: 14px" onclick="editingStatus(\'' + row[2] + '\',\'' + row[7] + '\',\'' + 2 + '\',\'' +row[11] + '\')">启用</a><a onclick="editPoint(\'' + row[11] + '\')" style="margin-right: 14px">编辑</a>'

                        } else {
                            val ='<a style="margin-right: 14px" onclick="deletes(\'' + row[2] + '\',\'' +row[7] + '\',\'' +row[11] + '\')">删除</a> <a style="margin-right: 14px" onclick="editingStatus(\'' + row[2] + '\',\'' + row[7] + '\',\'' + 1 + '\',\'' +row[11] + '\')">禁用</a><a onclick="editPoint(\'' + row[11] + '\')" style="margin-right: 14px">编辑</a>'

                        }

                        return val;
                    },
                    "targets": 10
                },

            ]
        });

    };
}
//总数量修改
function edit_stock(id,value) {
    $('#'+id+'').addClass('hide');
    $('#changeNum'+id+'').removeClass('hide');
    $('#change'+id+'').removeClass('hide');
    $('#cancel'+id+'').removeClass('hide');
    $('#editorIcon'+id+'').addClass('hide');
    $('#changeNum'+id+'').val($('#'+id+'').html());
}
//修改总数量
function isChange(id,productCount,ids) {
    $('#'+id+'').removeClass('hide');
    $('#changeNum'+id+'').addClass('hide');
    $('#change'+id+'').addClass('hide');
    $('#cancel'+id+'').addClass('hide');
    $('#editorIcon'+id+'').removeClass('hide');
    var virtualStockLimit=$('#changeNum'+id+'').val();
    if(virtualStockLimit==""){
        $.Huimodalalert('请填写总数量',2000);
        return  false;
    }
    $.ajax({
        url:root.integralManagement.updateIntegralActivity,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":ids,
            "productCount":productCount,
            "productTotal":virtualStockLimit,
            "updatePerson":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
        }),
        success: function (data) {
            if(data.errorCode==0){
                if(data.sub_code==1){
                    $.Huimodalalert('商品总数量不可低于可用数量',2000);
                }else {
                    $.Huimodalalert('成功！',2000);
                    IntegralActivity()
                }


            }else {

                $.Huimodalalert(data.msg,2000);

            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })

}
//取消
function isCancel(id,oldVal) {
    console.log($('#'+id+'').html())
    $('#'+id+'').removeClass('hide');
    $('#changeNum'+id+'').addClass('hide');
    $('#change'+id+'').addClass('hide');
    $('#cancel'+id+'').addClass('hide');

    $('#changeNum'+id+'').val($('#'+id+'').html());
    $('#editorIcon'+id+'').removeClass('hide');
}
//删除
function deletes(code,status,id) {
    var curInd = layer.confirm(
        '<span>确定删除吗？</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']}, function(index){

            $.ajax({
                url:root.integralManagement.forbiddenIntegralActivity,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "id":id,
                    "integralProductCode":code,
                    "activityStatus":50,
                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert('删除成功',2000);
                        IntegralActivity()

                    }else {

                        $.Huimodalalert(data.msg,2000);

                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            })
            layer.close(curInd);

        },function(){
            layer.close(curInd);

        });

}
//点击启用禁用
function editingStatus(code,status,hh,id) {
    $.ajax({
        url:root.integralManagement.forbiddenIntegralActivity,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":id,
            "integralProductCode":code,
            "activityStatus":status,
        }),
        success: function (data) {
            if(data.errorCode==0){
                if(hh==1){
                    $.Huimodalalert('禁用成功',2000);
                    IntegralActivity()
                }else {
                    $.Huimodalalert('启用成功',2000);
                    IntegralActivity()
                }


            }else {

                $.Huimodalalert(data.msg,2000);

            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })
}

//添加积分兑换商品
function addPoints() {
    var title="添加兑换商品"
    var url="IntegralActivity_addPoints.html"
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//活动规则编辑
function eventEditor() {
    var title="活动规则编辑"
    var url="IntegralActivity_eventEditor.html"
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//活动专题图上传
function pictureUpload() {
    var title="活动专题图上传"
    var url="IntegralActivity_pictureUpload.html"
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//编辑
function editPoint(id) {
    sessionStorage.setItem("id",id)
    var title="编辑"
    var url="IntegralActivity_editPoints.html"
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}